package com.yupi.mianshiya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.ThrowUtils;
import com.yupi.mianshiya.model.dto.learningPath.LearningPathQueryRequest;
import com.yupi.mianshiya.model.entity.LearningPath;
import com.yupi.mianshiya.model.entity.User;
import com.yupi.mianshiya.model.vo.FavoriteVO;
import com.yupi.mianshiya.model.vo.LearningPathVO;
import com.yupi.mianshiya.service.LearningPathService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "学习路径接口")
@RestController
@RequestMapping("/learning")
@Slf4j
public class LearningPathController {
    @Resource
    private LearningPathService learningPathService;

    @Resource
    private UserService userService;

    @ApiOperation("分页获取学习路线")
    @GetMapping("list/page")
    public BaseResponse<Page<LearningPathVO>> getLearningPathList(LearningPathQueryRequest learningPathQueryRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUserPermitNull(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        QueryWrapper<LearningPath> queryWrapper = learningPathService.getQueryWrapper(learningPathQueryRequest);
        int current = learningPathQueryRequest.getCurrent();
        int size = learningPathQueryRequest.getPageSize();
        Page<LearningPath> learningPathPage = new Page<>(current, size);
        Page<LearningPath> learningPathPageResult = learningPathService.page(learningPathPage, queryWrapper);
        List<LearningPath> learningPathList = learningPathPageResult.getRecords();


        List<LearningPathVO> collect = learningPathList.stream().map(learningPath -> {
            LearningPathVO learningPathVO = new LearningPathVO();
            BeanUtils.copyProperties(learningPath, learningPathVO);
            User user = userService.getById(learningPath.getUserId());
            learningPathVO.setUsername(user.getUserName());
            return learningPathVO;
        }).collect(Collectors.toList());

        Page<LearningPathVO> learningPathVOPage = new Page<>(current, size);
        BeanUtils.copyProperties(learningPathPageResult, learningPathVOPage);
        learningPathVOPage.setRecords(collect);


        return ResultUtils.success(learningPathVOPage);

    }

    @ApiOperation("管理员增加学习路线")
    @PostMapping("add")
    public BaseResponse addLearningPath(@RequestBody LearningPath learningPath, HttpServletRequest request) {

        ThrowUtils.throwIf(learningPath == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(learningPath.getTitle() == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(learningPath.getContent() == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        boolean admin = userService.isAdmin(loginUser);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        learningPath.setUserId(loginUser.getId());

        boolean save = learningPathService.save(learningPath);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
    @ApiOperation("管理员删除学习路线")
    @DeleteMapping("delete")
    public BaseResponse deleteLearningPath(Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        boolean admin = userService.isAdmin(request);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);

        //查询有没有
        LearningPath learningPath = learningPathService.getById(id);
        ThrowUtils.throwIf(learningPath == null, ErrorCode.OPERATION_ERROR,"没有该学习路线");

        boolean remove = learningPathService.removeById(id);
        ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @ApiOperation("管理员修改学习路线")
    @PostMapping("update")
    public BaseResponse updateLearningPath(@RequestBody LearningPath learningPath, HttpServletRequest request) {

        ThrowUtils.throwIf(learningPath == null, ErrorCode.PARAMS_ERROR);

        ThrowUtils.throwIf(learningPath.getTitle() == null, ErrorCode.PARAMS_ERROR);

        ThrowUtils.throwIf(learningPath.getContent() == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        boolean admin = userService.isAdmin(loginUser);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        //查询有没有
        LearningPath oldLearningPath = learningPathService.getById(learningPath.getId());
        ThrowUtils.throwIf(oldLearningPath == null, ErrorCode.OPERATION_ERROR,"没有该学习路线");



        learningPath.setEditTime(new Date());
        boolean update = learningPathService.updateById(learningPath);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
    @ApiOperation("根据id获取学习路线详情")
    @GetMapping("get")
    public BaseResponse<LearningPathVO> getLearningPath(Long id, HttpServletRequest request) {

        ThrowUtils.throwIf(id<=0||id==null, ErrorCode.PARAMS_ERROR);

        LearningPath learningPath = learningPathService.getById(id);
        ThrowUtils.throwIf(learningPath == null, ErrorCode.NOT_FOUND_ERROR);
        LearningPathVO learningPathVO = new LearningPathVO();
        BeanUtils.copyProperties(learningPath, learningPathVO);
        User user = userService.getById(learningPath.getUserId());
        learningPathVO.setUsername(user.getUserName());
        return ResultUtils.success(learningPathVO);
    }

}
